package com.ruoyi.system.service.impl.reseller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.entity.TotalResultMap;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.TimeStampUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.reseller.VsDramasReseller;
import com.ruoyi.system.mapper.reseller.VsDramasResellerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.reseller.VsDramasResellerOrderMapper;
import com.ruoyi.system.domain.reseller.VsDramasResellerOrder;
import com.ruoyi.system.service.reseller.IVsDramasResellerOrderService;

/**
 * 分销订单Service业务层处理
 * 
 * @author zzx
 * @date 2024-04-27
 */
@Service
public class VsDramasResellerOrderServiceImpl implements IVsDramasResellerOrderService 
{
    @Autowired
    private VsDramasResellerOrderMapper vsDramasResellerOrderMapper;

    @Autowired
    private VsDramasResellerMapper vsDramasResellerMapper;

    /**
     * 查询分销订单
     * 
     * @param id 分销订单主键
     * @return 分销订单
     */
    @Override
    public VsDramasResellerOrder selectVsDramasResellerOrderById(Integer id)
    {
        return vsDramasResellerOrderMapper.selectVsDramasResellerOrderById(id);
    }

    /**
     * 查询分销订单列表
     * 
     * @param vsDramasResellerOrder 分销订单
     * @return 分销订单
     */
    @Override
    public List<VsDramasResellerOrder> selectVsDramasResellerOrderList(VsDramasResellerOrder vsDramasResellerOrder)
    {
        return vsDramasResellerOrderMapper.selectVsDramasResellerOrderList(vsDramasResellerOrder);
    }

    /**
     * 新增分销订单
     * 
     * @param vsDramasResellerOrder 分销订单
     * @return 结果
     */
    @Override
    public String insertVsDramasResellerOrder(VsDramasResellerOrder vsDramasResellerOrder)
    {
        VsDramasReseller reseller = vsDramasResellerMapper.selectVsDramasResellerById(vsDramasResellerOrder.getId());
        VsDramasResellerOrder order = new VsDramasResellerOrder();
        String s = "R" + System.currentTimeMillis() + IdUtils.fastSimpleUUID();
        order.setOrderSn(s);
        order.setLangId(vsDramasResellerOrder.getLangId());
        order.setResellerId(reseller.getId());
        Long id = SecurityUtils.getLoginUser().getVsUser().getId();
        order.setUserId(Math.toIntExact(id));
        order.setTimes(reseller.getExpire());
        order.setStatus(0);
        order.setTotalFee(reseller.getPrice());
        order.setPayFee(BigDecimal.valueOf(0));

        Long expire = reseller.getExpire();
        if (0L == expire) {
            order.setTimes(0L);
        } else {
            int result = Math.toIntExact(expire) / 86400;
            long timeStampByDate = TimeStampUtils.getTimeStampByDate(result);
            order.setTimes(timeStampByDate);
        }

        order.setCreatetime(System.currentTimeMillis());
        order.setUpdatetime(System.currentTimeMillis());
        int i = vsDramasResellerOrderMapper.insertVsDramasResellerOrder(order);
        if (i > 0) {
            return s;
        }
        return "err";
    }

    /**
     * 修改分销订单
     * 
     * @param vsDramasResellerOrder 分销订单
     * @return 结果
     */
    @Override
    public int updateVsDramasResellerOrder(VsDramasResellerOrder vsDramasResellerOrder)
    {
        vsDramasResellerOrder.setUpdatetime(System.currentTimeMillis());
        return vsDramasResellerOrderMapper.updateVsDramasResellerOrder(vsDramasResellerOrder);
    }

    /**
     * 批量删除分销订单
     * 
     * @param ids 需要删除的分销订单主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasResellerOrderByIds(Integer[] ids)
    {
        return vsDramasResellerOrderMapper.deleteVsDramasResellerOrderByIds(ids);
    }

    /**
     * 删除分销订单信息
     * 
     * @param id 分销订单主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasResellerOrderById(Integer id)
    {
        return vsDramasResellerOrderMapper.deleteVsDramasResellerOrderById(id);
    }

    @Override
    public TotalResultMap selectCount(VsDramasResellerOrder vsDramasResellerOrder) {
        return vsDramasResellerOrderMapper.selectCount(vsDramasResellerOrder);
    }
}
